﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Data.SqlClient;
using System.IO;   //输入输出。













namespace 共享单车管理系统.star.Tgirbut
{
    public partial class Dleter_prod : Form
    {
        public Dleter_prod()
        {
            InitializeComponent();
        }
        SqlDataAdapter daProduct, daLog;
        DataSet ds = new DataSet();
        void init()
        {
            DB.GetCn();
            string str = "select * from Bike";
            string sdr = "select * from log";
            daProduct = new SqlDataAdapter(str, DB.cn);
            daLog = new SqlDataAdapter(sdr, DB.cn);
            daProduct.Fill(ds, "product_info");
            daLog.Fill(ds, "log_info");
        }


        void showAll()
        {
            DataView dvProduct = new DataView(ds.Tables["product_info"]);
            dvProducts.DataSource = dvProduct;

        }

        private void buttondelet_Click(object sender, EventArgs e)
        {
            int s =dvProducts.Rows.Count;
            for (int i = 0; i < dvProducts.Rows.Count; i++)
            {
                if (dvProducts.Rows[i].Cells["acCode"].EditedFormattedValue.ToString() == "True")
                {
                    DialogResult dr = MessageBox.Show("您确定要对此商品进行删除吗", "提示",
                        MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    if (dr == DialogResult.OK)
                    {
                        dvProducts.Rows.RemoveAt(i);
                        //添加日志
                        DataRow drlog = ds.Tables["log_info"].NewRow();
                        drlog["username"] = Login.username;
                        drlog["type"] = "删除";
                        drlog["Time"] = DateTime.Now;
                        drlog["table"] = "commodity表";
                        ds.Tables["log_info"].Rows.Add(drlog);

                        UpdateDB();
                    }

                    else
                    {
                        return;
                    }
                }
                     else
                    {
                    s = s - 1;

                    }

            }
            if (s == 0)
            {
                MessageBox.Show("请选择你要删除的商品");
            }
        }
        void UpdateDB()
        {
            try
            {
                SqlCommandBuilder dbProduct = new SqlCommandBuilder(daProduct);
               daProduct.Update(ds, "product_info");
                SqlCommandBuilder dblog = new SqlCommandBuilder(daLog);
                daLog.Update(ds, "log_info");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            DB.cn.Close();
        }

        private void Dleter_prod_Load(object sender, EventArgs e)
        {
            showX2();
            init();
            showAll();
            
        }

        private void dvProducts_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dvProducts.Rows.Count > 0)
            {
                for (int i = 0; i < dvProducts.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell ck = dvProducts.Rows[i].Cells["acCode"] as DataGridViewCheckBoxCell;
                    if (i != e.RowIndex)
                    {
                        ck.Value = false;
                    }
                    else
                    {
                        ck.Value = true;
                    }
                }


            }
        }

        void showX2()
        {
            DataGridViewCheckBoxColumn acCode = new DataGridViewCheckBoxColumn();
            acCode.Name = "acCode";
            acCode.HeaderText = "选择";
            dvProducts.Columns.Add(acCode);
        }

    }
}
